//
//  CustomCalloutView.m
//  Category_demo2D
//
//  Created by xiaoming han on 13-5-22.
//  Copyright (c) 2013年 songjian. All rights reserved.
//

#import "CustomCalloutView.h"
#import <QuartzCore/QuartzCore.h>
#import "EqpInfo.h"
#import "AppDelegate.h"
#import "EqpInfoTabController.h"
#import "DateUtils.h"

#define kArrorHeight    10

@interface CustomCalloutView () {
    
    __weak IBOutlet UILabel *eqpNameLabel;
    __weak IBOutlet UILabel *statusLabel;
    __weak IBOutlet UILabel *timeLabel;
    __weak IBOutlet UILabel *addressLabel;
    
    __weak IBOutlet UIButton *closeButton;
    __weak IBOutlet UIButton *followButton;
    __weak IBOutlet UIButton *viewHistoryButton;
    __weak IBOutlet UIButton *setEqpButton;
}

@end

@implementation CustomCalloutView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

#pragma mark - setter 
- (void)setEqpInfo:(EqpInfo *)eqpInfo {
    _eqpInfo = eqpInfo;
    

    NSString *PositionTime = eqpInfo.PositionTime == nil ? @"":eqpInfo.PositionTime;
    if (![PositionTime isEqualToString:@""]) {
//        PositionTime = [DateUtils dataFromString:PositionTime fromFormat:@"yyyy'/'M'/'d hh:mm:ss" toFormat:@"yyyy年MM月dd日 hh:mm:ss"];
    }


    NSString *eqpName = eqpInfo.EqpName == nil ? @"":eqpInfo.EqpName;
    NSString *address = eqpInfo.Address==nil ? @"暂无":eqpInfo.Address;
    NSString *speedStr = [eqpInfo.Speed integerValue]==0 ? @"静止":@"行驶";
    
    
    eqpNameLabel.text = [NSString stringWithFormat:@"%@", eqpName];
    statusLabel.text = [NSString stringWithFormat:@"状态：%@", speedStr];
    timeLabel.text = [NSString stringWithFormat:@"时间：%@", PositionTime];
    addressLabel.text = [NSString stringWithFormat:@"地址：%@", address];
}

- (void)addCloseButtonTarget:(id)target action:(SEL)sel {
    [closeButton addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
}

#pragma event 
- (IBAction)closeButtonPress:(id)sender {
//    self.hidden = YES;

}
- (IBAction)followButtonPress:(id)sender {
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    UINavigationController *rootNavVC = appDelegate.navigationController;
    EqpInfoTabController *toController = [[EqpInfoTabController alloc] init];
    toController.eqpInfo = self.eqpInfo;
    toController.title = self.eqpInfo.EqpName;
    toController.selectedIndex = 1;
    [rootNavVC pushViewController:toController animated:YES];
}
- (IBAction)historyButtonPress:(id)sender {
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    UINavigationController *rootNavVC = appDelegate.navigationController;
    EqpInfoTabController *toController = [[EqpInfoTabController alloc] init];
    toController.eqpInfo = self.eqpInfo;
    toController.title = self.eqpInfo.EqpName;
    toController.selectedIndex = 2;
    [rootNavVC pushViewController:toController animated:YES];
}
- (IBAction)setButtonPress:(id)sender {
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    UINavigationController *rootNavVC = appDelegate.navigationController;
    EqpInfoTabController *toController = [[EqpInfoTabController alloc] init];
    toController.eqpInfo = self.eqpInfo;
    toController.title = self.eqpInfo.EqpName;
    toController.selectedIndex = 3;
    [rootNavVC pushViewController:toController animated:YES];
    
}
- (IBAction)infoBgTap:(id)sender {
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    UINavigationController *rootNavVC = appDelegate.navigationController;
    EqpInfoTabController *toController = [[EqpInfoTabController alloc] init];
    toController.eqpInfo = self.eqpInfo;
    toController.title = self.eqpInfo.EqpName;
    toController.selectedIndex = 0;
    [rootNavVC pushViewController:toController animated:YES];
}

#pragma mark - draw rect

- (void)drawRect:(CGRect)rect
{
    
    [self drawInContext:UIGraphicsGetCurrentContext()];
    
    self.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.layer.shadowOpacity = 1.0;
    self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
    
}

- (void)drawInContext:(CGContextRef)context
{
    
    CGContextSetLineWidth(context, 2.0);
    CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.6].CGColor);
    
    [self getDrawPath:context];
    CGContextFillPath(context);
    
}

- (void)getDrawPath:(CGContextRef)context
{
    CGRect rrect = self.bounds;
    CGFloat radius = 6.0;
    CGFloat minx = CGRectGetMinX(rrect),
    midx = CGRectGetMidX(rrect),
    maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect),
    maxy = CGRectGetMaxY(rrect)-kArrorHeight;
    
    CGContextMoveToPoint(context, midx+kArrorHeight, maxy);
    // 三角
    CGContextAddLineToPoint(context,midx, maxy+kArrorHeight);
    CGContextAddLineToPoint(context,midx-kArrorHeight, maxy);
    
    // 矩形
    CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);
    CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, maxx, radius);
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    CGContextClosePath(context);
}

@end
